package com.wctj.api.business.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wctj.api.base.entity.DdwxPaymentRecord;
import com.wctj.api.base.utils.InstanceConvertUtil;
import com.wctj.api.business.common.dao.DdwxPaymentRecordMapper;
import com.wctj.api.business.common.dto.DdwxPaymentRecordDTO;
import com.wctj.api.business.common.dto.PaymentRecordDTO;
import com.wctj.api.business.common.service.DdwxPaymentRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class DdwxPaymentRecordServiceImpl extends ServiceImpl<DdwxPaymentRecordMapper, DdwxPaymentRecord> implements DdwxPaymentRecordService {


    @Override
    public DdwxPaymentRecordDTO findByPayOrderNumber(String payOrderNumber) {
        DdwxPaymentRecord record = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DdwxPaymentRecord::getPayOrderNum, payOrderNumber)
                .one();
        return InstanceConvertUtil.convertObject(record, DdwxPaymentRecordDTO.class);
    }

    @Override
    public void updateById(DdwxPaymentRecordDTO recordDTO) {
       Boolean updateBool = this.updateById(InstanceConvertUtil.convertObject(recordDTO, DdwxPaymentRecord.class));
       log.info("updateById(recordDTO) effect：{}", updateBool);
    }

    @Override
    public DdwxPaymentRecordDTO findByPayNumAndType(String yurref, String type) {
        DdwxPaymentRecord record = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DdwxPaymentRecord::getPayNum, yurref)
                .eq(DdwxPaymentRecord::getPayType, type)
                .one();
        return InstanceConvertUtil.convertObject(record, DdwxPaymentRecordDTO.class);
    }

    @Override
    public Page<DdwxPaymentRecordDTO> getPaymentRecordList(PaymentRecordDTO paymentRecordDTO) {
        //分页参数
        Page<DdwxPaymentRecord> rowPage = new Page(paymentRecordDTO.getCurrentPage(), paymentRecordDTO.getPageSize());
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<DdwxPaymentRecord> queryWrapper = buildWrapper(paymentRecordDTO);
        rowPage = baseMapper.selectPage(rowPage, queryWrapper);
        List<DdwxPaymentRecordDTO> ddwxPaymentRecordDTOS = InstanceConvertUtil.convertList(rowPage.getRecords(), DdwxPaymentRecordDTO.class);
        Page page = InstanceConvertUtil.convertObject(rowPage, Page.class);
        page.setRecords(ddwxPaymentRecordDTOS);
        return page;
    }

    public LambdaQueryWrapper buildWrapper(PaymentRecordDTO paymentRecordDTO) {
        LambdaQueryWrapper<DdwxPaymentRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(paymentRecordDTO.getBizCode())) {
            queryWrapper.eq(DdwxPaymentRecord::getBizCode, paymentRecordDTO.getBizCode());
        }
        if (StringUtils.isNotBlank(paymentRecordDTO.getAppId())) {
            queryWrapper.eq(DdwxPaymentRecord::getAppId, paymentRecordDTO.getAppId());
        }
        if (StringUtils.isNotBlank(paymentRecordDTO.getPlatform())) {
            queryWrapper.eq(DdwxPaymentRecord::getPlatform, paymentRecordDTO.getPlatform());
        }
        if (StringUtils.isNotBlank(paymentRecordDTO.getBizOrderNum())) {
            queryWrapper.eq(DdwxPaymentRecord::getBizOrderNum, paymentRecordDTO.getBizOrderNum());
        }
        if (StringUtils.isNotBlank(paymentRecordDTO.getPayType())) {
            queryWrapper.eq(DdwxPaymentRecord::getPayType, paymentRecordDTO.getPayType());
        }
        if (StringUtils.isNotBlank(paymentRecordDTO.getPayOrderNum())) {
            queryWrapper.eq(DdwxPaymentRecord::getPayOrderNum, paymentRecordDTO.getPayOrderNum());
        }
        if (StringUtils.isNotBlank(paymentRecordDTO.getPayChannel())) {
            queryWrapper.eq(DdwxPaymentRecord::getPayChannel, paymentRecordDTO.getPayChannel());
        }
        if (Objects.nonNull(paymentRecordDTO.getPayTime())) {
            queryWrapper.eq(DdwxPaymentRecord::getPayTime, paymentRecordDTO.getPayTime());
        }
        if (Objects.nonNull(paymentRecordDTO.getStatus())) {
            queryWrapper.eq(DdwxPaymentRecord::getStatus, paymentRecordDTO.getStatus());
        }
        return queryWrapper;
    }

}
